{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" \n",
    "数据读取练习\n",
    " \"\"\"\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 打开本地CSV文件 \"\"\"\n",
    "pd.read_csv(R\"G:\\Users\\yangjh\\Desktop\\repos\\statistic-2022\\data\\movie.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 打开存放在网络中的公开csv文件 \"\"\"\n",
    "fileUrl = R\"https://oss-yangjh.oss-cn-chengdu.aliyuncs.com/data/csv/600519.csv\"\n",
    "df = pd.read_csv(fileUrl,nrows=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 打开Excel格式的文件 \"\"\"\n",
    "file_url = R'G:\\Users\\yangjh\\Desktop\\repos\\statistic-2022\\data\\types-of-variables.xlsx'\n",
    "df_jobs = pd.read_excel(file_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 保存CSV文件 \"\"\"\n",
    "# 保存到csv文件，建议将index关闭。\n",
    "df_jobs.to_csv(r\"temp/job.csv\",index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入pyreadstat包，pandas也使用这个包来打开商业分析软件（如SPSS、STATA）的文件。\n",
    "# 在<https://github.com/Roche/pyreadstat>可查阅该包的详细文档。\n",
    "import pyreadstat\n",
    "# pyreadstat.read_sav方法有几个重要参数：\n",
    "# apply_value_formats 默认为False，如果我们想要标签描述（即变量中的选项名称，如喜欢、不喜欢），而不仅仅是数字的话（即变量在SPSS中的数值，通常为1、2……），需要将这个参数设置为True。\n",
    "# formats_as_category 默认为True, 意味着读入到Pandas时会将变量转化为category类型的列。\n",
    "# formats_as_ordered_category 默认为False，需要将其设置为True，这样pandas在读取时，会保留在SPSS中定义的变量测量层次。\n",
    "# 该函数返回的值是一个元组，第一个元素为DataFrame类型，第二个元素为整个表变量的定义信息。\n",
    "# df, metadata = pyreadstat.pyreadstat.read_sav(r'data/identity.sav',\n",
    "#                                               formats_as_ordered_category=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df, metadata = pyreadstat.pyreadstat.read_sav(r'data/identity.sav',\n",
    "                                              formats_as_ordered_category=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "905"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取行数\n",
    "metadata.number_rows\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
